<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Shop extends AdminBackEndController {
    public function __construct() {
        parent::__construct();
        $this->load->model('repo/shop_model', 'shop_model');
        $this->load->library('mypaging');
    }

    private function intialPaging() {
        $this->mypaging->setCount($this->shop_model->countShop());
        $this->mypaging->setLimit(SHOP_LIMIT);
        $this->mypaging->setMainURL(admin_shop_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function index($pageIndex = 1) {
        $this->intialPaging();
        $offset = ($pageIndex - 1) * SHOP_LIMIT;
        $pagingBar = $this->mypaging->getBar($pageIndex);

        $aShop = $this->shop_model->paging(
            SHOP_LIMIT,
            $offset,
            array(
                'a.created_at AS shop_created_at',
                'a.deleted_at AS shop_deleted_at',
                'c.full_name AS fullName'
                ),
            true,
            array(
                'a.active_flg' => STATUS_ACTIVE,
                'c.deleted_at' => NULL
                )
            );
        $aReturn = array(
            'pageTitle' => 'Gian hàng',
            'aShop' => $aShop,
            'pagingBar' => $pagingBar);
        $this->renderView('backend/admin/shop/index', $aReturn);
    }

    public function changeStatus() {
        $id       = 0;
        $deleteAt = null;
        $aData    = array();
        $aResult  = array();
        $shop     = $this->input->post('shop');
        if (isset($shop['id'])) {
            $id = $this->encrypt->decode($shop['id']);
            if ($id === false) {
                $aResult = array(
                    'code' => 0,
                    'text' => $this->lang->line('shop_info_invalid')
                    );
            }
        } else {
            $aResult = array(
                'code' => 0,
                'text' => $this->lang->line('shop_info_invalid'),
                );
        }
        if(count($aResult)>0){
            $this->renderJSON($aResult);
            return;
        }
        if (!isset($shop['delete_flg']) && !isset($shop['highlight_flg'])) {
            $aResult = array(
                'code' => 0,
                'text' => $this->lang->line('shop_info_invalid')
                );
            $this->renderJSON($aResult);
            return;
        } else {
            $date = new DateTime('now');
            if (isset($shop['delete_flg']) && ($shop['delete_flg'] == 1 || $shop['delete_flg'] == 0)) {
                $aData['deleted_at'] = $date->format(DATE_TIME_FORMAT);
            }
            if (isset($shop['highlight_flg']) && ($shop['highlight_flg'] == 1 || $shop['highlight_flg'] == 0)) {
                $aData['highlight_flg'] = $shop['highlight_flg'];
                $aData['updated_at'] = $date->format(DATE_TIME_FORMAT);
            }
            if (count($aData) == 0) {
                $aResult = array(
                    'code' => 0,
                    'text' => $this->lang->line('shop_info_invalid')
                    );
                $this->renderJSON($aResult);
                return;
            }
        }
        $result = $this->shop_model->update($id, $aData);
        if ($result) {
            $aResult = array(
                'code' => 1,
                'text' => $this->lang->line('update_success'),
                );
        } else {
            $aResult = array(
                'code' => 0,
                'title' => '<ul><li>' . $this->lang->line('system_error') . '</li></ul>'
                );
        }
        $this->renderJSON($aResult);
    }

    /// show owner area
    public function get($slug) {
        $this->load->helper('file');
        $shop = $this->shop_model->getShopBySlug($slug, array('b.banner as banner'), true);
        if ($shop == null || count($shop) == 0) {
            show_404();
        }
        $aReturn = array(
            'pageTitle' => $shop->getTitle(),
            'shop' => $shop,
            'action_link' => admin_page_url(SHOP_SLUG . '/cap-nhat'));
        $this->renderView('backend/user/shop-single', $aReturn);
    }

    private function intialPagingNewRegist() {
        $this->mypaging->setCount(
            $this->shop_model->countShop(
                array(
                    'a.active_flg' => STATUS_PENDING,
                    'a.deleted_at' => NULL,
                    'c.deleted_at' => NULL
                    ),
                false
                )
            );
        $this->mypaging->setLimit(SHOP_LIMIT);
        $this->mypaging->setMainURL(admin_shop_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function newRegister($pageIndex = 1) {
        $this->intialPagingNewRegist();
        $offset = ($pageIndex - 1) * SHOP_LIMIT;
        $pagingBar = $this->mypaging->getBar($pageIndex);
        $aShop = $this->shop_model->paging(
            SHOP_LIMIT,
            $offset,
            array(
                'a.created_at AS shop_created_at',
                'a.deleted_at AS shop_deleted_at',
                'c.id as user_id, c.full_name AS full_name'
                ), true,
            array(
                'a.active_flg' => STATUS_PENDING,
                'a.deleted_at' => NULL,
                'c.deleted_at' => NULL
                )
            );
        $aReturn = array(
            'pageTitle' => 'Gian hàng',
            'aShop' => $aShop,
            'pagingBar' => $pagingBar);
        $this->renderView('backend/admin/shop/new-register', $aReturn);
    }

    public function acceptShopRegister() {
        $id = 0;
        $activeFlg = 0;
        $userId = 0;
        $aResult = array();
        $shop = $this->input->post('shop');

        if (isset($shop['id'])) {
            $id = $this->encrypt->decode($shop['id']);
            if ($id === false) {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('shop_active_fail'),
                    'text' => '<ul><li>' . $this->lang->line('shop_id_invalid') . '</li></ul>',
                    );
            }
            if (!isset($shop['user_id'])) {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('shop_active_fail'),
                    'text' => '<ul><li>' . $this->lang->line('shop_active_flg_invalid') .'</li></ul>',
                    );
            } else {
                $userId = $this->encrypt->decode($shop['user_id']);
                if ($userId === false) {
                    $aResult = array(
                        'code' => 0,
                        'title' => $this->lang->line('shop_active_fail'),
                        'text' => '<ul><li>' . $this->lang->line('shop_id_invalid') . '</li></ul>',
                        );
                }
            }
        } else {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('shop_active_fail'),
                'text' => '<ul><li>' . $this->lang->line('shop_id_invalid') . '</li></ul>',
                );
        }
        if (count($aResult) == 0) {
            $date = new DateTime();
            $objShop = array();
            $objShop['id'] = $id;
            $objShop['user_id'] = $userId;
            $objShop['updated_at'] = $date->format('Y-m-d H:i:s');
            $result = $this->shop_model->acceptShopRegister($objShop);
            if ($result) {
                $aResult = array(
                    'code' => 1,
                    'text' => $this->lang->line('update_success')
                    );
            } else {
                $aResult = array(
                    'code' => 0,
                    'title' => $this->lang->line('update_fail'),
                    'text' => '<ul><li>' . $this->lang->line('system_error') . '</li></ul>'
                    );
            }
        }

        $this->renderJSON($aResult);
    }

    public function changeBlockProductNum() {
        $aResult = array();
        $shop = $this->input->post('shop');
        if(!isset($shop['id']) || !isset($shop['num'])){
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
                );
            $this->renderJSON($aResult);
            return;
        }
        $id = $this->encrypt->decode($shop['id']);
        if($id===false){
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
                );
            $this->renderJSON($aResult);
            return;
        }
        $options = array('id' => $id, 'num'=>$shop['num'], 'updated_at'=>get_current_date());
        $result = $this->shop_model->updateBlockProductNum($options);
        if ($result) {
            $aResult = array(
                'code' => 1,
                'text' => $this->lang->line('update_success'),
                );
        } else {
            $aResult = array(
                'code' => 0,
                'title' => $this->lang->line('system_error')
                );
        }
        $this->renderJSON($aResult);
    }

    private function intialSearchPaging($searchkey) {
        $this->mypaging->setCount($this->shop_model->countSearchPaging($searchkey));
        $this->mypaging->setLimit(SHOP_LIMIT);
        $this->mypaging->setMainURL(admin_shop_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function search($pageIndex=1){
        $searchkey = $this->input->get('s', true);
        $this->intialSearchPaging($searchkey);
        $offset = ($pageIndex - 1) * SHOP_LIMIT;
        $pagingBar = $this->mypaging->getBar($pageIndex);
        $aShop = $this->shop_model->searchPaging(
            $offset,
            SHOP_LIMIT,
            array(
                'a.created_at AS shop_created_at',
                'a.deleted_at AS shop_deleted_at',
                'c.full_name AS fullName'
                ),
            $searchkey
            );
        $aReturn = array(
            'pageTitle' => 'Tìm kiếm | Gian hàng',
            'aShop'     => $aShop,
            'pagingBar' => $pagingBar,
            'searchkey' => $searchkey
            );
        $this->renderView('backend/admin/shop/index', $aReturn);
    }
}
?>